package com.shire.shire_interface.service.commandcenter.queues;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.constants.WxConstants;
import com.shire.shire_interface.model.command.WxCommand;
import com.shire.shire_interface.model.user.WxAccount;
import com.shire.shire_interface.model.validation.WxValidationRecord;
import com.shire.shire_interface.model.validation.WxValidationRule;
import com.shire.shire_interface.service.commandcenter.WxCommandService;
import com.shire.shire_interface.service.validation.WxValidationRecordService;
import com.shire.shire_interface.service.validation.WxValidationRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class ValidationCommandQueue extends GappedCommandQueue {

    @Autowired
    private WxValidationRuleService wxValidationRuleService;

    @Autowired
    private WxValidationRecordService wxValidationRecordService;

    @Autowired
    private WxCommandService commandService;

    private Random rand;

    public ValidationCommandQueue(WxAccount wxAccount, WxConstants.CmdPriority priority, WxValidationRuleService wxValidationRuleService, WxValidationRecordService wxValidationRecordService, WxCommandService commandService) {
        super(wxAccount, priority);
        this.wxValidationRuleService = wxValidationRuleService;
        this.wxValidationRecordService = wxValidationRecordService;
        this.commandService = commandService;
        lastReturnTime = new Date();
        rand = new Random((new Date()).getTime());
    }

    private static ValidationCommandQueue instance;

    @PostConstruct
    public void init(){
        instance = this;

    }

    public static ValidationCommandQueue getInstance(){
        return instance;
    }

    public ValidationCommandQueue getQueueForAccount(WxAccount account){

        return new ValidationCommandQueue(account, WxConstants.CmdPriority.UNKNOWN, this.wxValidationRuleService,this.wxValidationRecordService,this.commandService);
    }

    public ValidationCommandQueue(){
        super();
    }


    @Override
    public WxCommand getNextCommand() {
        synchronized (this.wxAccount.getWxid().intern()) {
            if ((new Date()).getTime() < lastReturnTime.getTime()) //未到下次指令时间
                return null;

            WxValidationRule rule = wxValidationRuleService.getValidationRuleForCs(wxAccount.getWxid());
            if (rule == null)
                return null;

            if (rule.getAutopass() == 0) {//手动状态，不处理
                return null;
            }
            Integer secondCnt = 180;
            if (rule.getSleepstart() != null && rule.getSleepend() != null) {
                secondCnt = rand.nextInt(rule.getSleepend() - rule.getSleepstart());
            }

            List<String> accountList = new ArrayList<>();
            accountList.add(wxAccount.getWxid());
            List<WxValidationRecord> records = wxValidationRecordService.getValidationRecord(wxAccount.getCsid(),
                    accountList,
                    WxConstants.ValidationStatus.WAIT);
            if (records == null || records.size() < 1)
                return null;

            WxValidationRecord record = records.get(0);
            JSONObject firstMsg = (JSONObject) JSONObject.parseArray(record.getMsglist()).get(0);

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("ticket", firstMsg.getString("ticket"));

            curCmd = commandService.generateCmdObject(wxAccount.getCsid(), wxAccount.getId(), jsonObject.toJSONString(), WxConstants.CmdTaskType.VALIDATION, record.getId(), WxConstants.CmdType.CRK_HELLO_VERIFY_D, WxConstants.CmdPriority.UNKNOWN);
            record.setCmdid(curCmd.getId().intValue());
            record.setCmdstatus(curCmd.getRststatus());
            record.setUpdateTime(new Date());
            wxValidationRecordService.updateRecord(record);
            lastReturnTime = new Date((new Date()).getTime() + secondCnt * 1000);
        }
        return super.getNextCommand();
    }
}
